<?php
/**
 * Created by PhpStorm.
 * User: openote
 * Date: 2018/12/10
 * Time: 18:57
 */

namespace app\admin\controller;


use app\admin\model\Cadre;
use app\admin\model\Evaluations;
use app\admin\model\EvaluationsLog;
use app\admin\model\EvaluationsQuestion;
use app\admin\model\Teacher;
use think\Exception;
use think\Request;

class EvaluationsController extends BaseController {

    public function index() {
        $e = Evaluations::paginate(10);
        $this->assign('e', $e);
        return view('index');
    }


    public function add(Request $request) {
        if ($request->isPost()) {
            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\Evaluations');
            if (true !== $result)
                return $this->error($result, 'add');

            $param['status'] = 2;
            $param['addtime'] = time();
//            $param['classid'] = join(',', $param['classid']);
            $res = Evaluations::create($param);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        } else {
            return view('add');
        }
    }

    public function edit(Request $request, $id) {
        $evaluations = Evaluations::get($id);
        if ($request->isPost()) {
            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\Evaluations');
            if (true !== $result)
                return $this->error($result, 'add');
//            $param['classid'] = join(',', $param['classid']);
            $res = $evaluations->isUpdate(true)->save($param);
            if ($res) {
                return $this->success('编辑成功', 'index');
            } else {
                return $this->error('编辑失败', 'add');
            }
        } else {
            $this->assign('e', $evaluations);
            return view('edit');
        }
    }

    public function status(Request $request) {
        $param = $request->post();
        $e = Evaluations::get($param['id']);
        if (!$e) {
            return json(['message'=>'该评教不存在', 'status'=>404]);
        }
        if ($e['status'] == 2) {
            // 清空其他的 status 为1 的状态
            Evaluations::update(['status'=>2], ['status'=>1]);
        }
        if ($param['status'] === 0) $param['status'] = 2;
        $res = Evaluations::update(['status'=>$param['status']], ['id'=>$param['id']]);
//        $res = $e->isUpdate(true)->save($param);
        if ($res) {
            return json(['message'=>'修改成功', 'status'=>200]);
        } else {
            return json(['message'=>'修改失败', 'status'=>404]);
        }
    }

    public function logs($eid) {
        $e = Evaluations::get($eid);
        $log = EvaluationsLog::where('eid', $eid)
            ->group('teacher_id')
            -> with('teacher')
            ->field(['id', 'eid', 'teacher_id', 'addtime'])
            ->paginate(10);

        $this->assign('logs', $log);
        $this->assign('e', $e);
        return view('logs');
    }

    public function cadreList($eid, $teacher_id) {
        $cadre = EvaluationsLog::where(['eid'=>$eid, 'teacher_id'=>$teacher_id])
            -> with('cadre')
            -> paginate(10);
        $teacher = Teacher::get($teacher_id);
        $e = Evaluations::get($eid);

        $this->assign('teacher', $teacher);
        $this->assign('cadre', $cadre);
        $this->assign('e', $e);
        return view('cadre_list');
    }

    public function questions($eid, $teacher_id, $uid){
        $q = EvaluationsLog::where(['eid'=>$eid, 'teacher_id'=>$teacher_id, 'uid'=>$uid])
            -> with('questions')
            -> select();
        $teacher = Teacher::get($teacher_id);
        $e = Evaluations::get($eid);
        foreach ($q as $key=>$item) {
            if ($item['questions']) {
                $q[$key]['questions']['cotent'] = json_decode($item['questions']['cotent'], true);
            }
        }
//        echo json_encode($q);
        $this->assign('q_count', (count($q)+1));
        $this->assign('teacher', $teacher);
        $this->assign('e', $e);
        $this->assign('q', $q);
//        print_r(collection($q)->toArray());
       /* foreach ($q as $key=>$item) {
            $a = $item['questions']['cotent'];
            foreach ($a as $k=>$v) {
                echo $a."=>".$v."</br>";
            }
        }*/
        return view('questions');

    }
}